package com.mby.controller;

import com.mby.entity.Dealer;
import com.mby.entity.User;
import com.mby.entity.WelcomeWords;
import net.dongliu.requests.Header;
import net.dongliu.requests.Requests;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by noah on 2018/4/15.
 */
@RestController
public class DealerController extends BaseController{

    /**
     *
     */
    @GetMapping(value = "/dealers")
    public void getDealers(String search_key) throws Exception{
        Map<String,String> dealers = new HashMap<>();
        List<Dealer> list = (List<Dealer>) mongoTool.findMany("{'cn.name*':'"+search_key+"'}","",Dealer.class);
        if(list.size() > 0){
            for(Dealer dealer : list){
                dealers.put(dealer.getId(),dealer.getCn().get("name").toString());
            }
        }

        list = (List<Dealer>) mongoTool.findMany("{'id*':'"+search_key+"'}","",Dealer.class);
        if(list.size() > 0){
            for(Dealer dealer : list){
                dealers.put(dealer.getId(),dealer.getCn().get("name").toString());
            }
        }

        respData(dealers);
    }

    /**
     * 获取经销商的欢迎语和结束语
     * get /live/welcome_words
     * @throws Exception
     */
    @GetMapping(value = "/welcome_words")
    public void getWelcomeWords() throws Exception{
        WelcomeWords welcomeWords = mongoTool.findOne("","{'dealerId':'"+currentUser().getDealerId()+"'}",WelcomeWords.class);
        if(welcomeWords == null){
            welcomeWords = new WelcomeWords();
            welcomeWords.setDealerId(currentUser().getDealerId());
            mongoTool.addOne(welcomeWords);
        }
        respData(welcomeWords);
    }

    /**
     * 修改经销商的欢迎语和结束语
     * put /live/welcome_words
     * @param welcomeWords
     * @throws Exception
     */
    @RequiresRoles(value={"10061025","10061017"},logical = Logical.OR)
    @PutMapping(value = "/welcome_words")
    public void updateWelcomeWords(@RequestBody WelcomeWords welcomeWords) throws Exception{
        if(welcomeWords.getWelcomeWords() == null){
            welcomeWords.setWelcomeWords("");
        }
        if(welcomeWords.getOverWords() == null){
            welcomeWords.setOverWords("");
        }
        mongoTool.findAndUpdate(welcomeWords,"{'dealerId':'"+currentUser().getDealerId()+"'}",WelcomeWords.class);
        respData(ok);
    }

    /**
     * 初始化欢迎语
     * get /live/init/welcome_words
     * @throws Exception
     */
    @GetMapping(value = "/init/welcome_words")
    public void initWelcomeWords() throws Exception{
        List<?> list = mongoTool.findMany("","","",Dealer.class);
        if(list.size() > 0){
            Dealer dealer; WelcomeWords welcomeWords;
            for(int i=0;i<list.size();i++){
                dealer = (Dealer)list.get(i);
                mongoTool.delete("{'dealerId':'"+dealer.getId()+"'}",WelcomeWords.class);
                welcomeWords = new WelcomeWords();
                welcomeWords.setDealerId(dealer.getId());
                welcomeWords.setWelcomeWords("尊贵的客户，您好！欢迎您体验捷豹路虎保养服务直播间，您将了解到实时透明的在线服务信息。");
                mongoTool.addOne(welcomeWords);
            }
        }
        respData(list.size());
    }
}
